package com.douglas.managesystem.controller.system;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.controller.BaseController;
import com.douglas.managesystem.entity.system.Menu;
import com.douglas.managesystem.service.system.MenuService;
import com.douglas.managesystem.vo.UserVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @Description 菜单管理入口
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-14 13:13
 **/
@Controller
@RequestMapping("/system")
public class MenuController extends BaseController {


    @Autowired
    private MenuService menuService;

    /**
     * 1.菜单列表查询
     * @param model
     * @return
     */
    @GetMapping("/menu/list")
    public String list(Model model, SearchEntity params){
        PageInfo<Menu> page = menuService.selectAllMenuByPage(params);
        model.addAttribute("menu",page);
        return "admin/menu/list";
    }

    /**
     * 2.去新增页面
     * @return
     */
    @GetMapping("/menu/toAdd")
    public String toAdd(){
        return "admin/menu/add";
    }

    /**
     * 3.去编辑页面
     * @param id
     * @return
     */
    @GetMapping("/menu/toEdit")
    public String toEdit(String id,Model model){
        Menu menu = menuService.selectMenuById(id);
        model.addAttribute("menu",menu);
        return "admin/menu/edit";
    }

    /**
     * 4.新增或编辑菜单
     * @param menu
     * @return
     */
    @PostMapping("/menu/add")
    public String addOrEdit(Menu  menu){
        UserVO user = (UserVO)session.getAttribute("loginUser");
        if(StringUtils.isEmpty(menu.getMenuId())){
            menu.setCreateId(user.getId());
            //没有ID新增
            menuService.addMenu(menu);
        }else{
            //有ID更新
            menu.setUpdateId(user.getId());
            menuService.editMenu(menu);
        }

        return "redirect:/system/menu/list";
    }

    /**
     * 5.删除菜单
     * @param id
     * @return
     */
    @GetMapping("/menu/delete")
    public String delete(String id){
        menuService.delete(id);
        return "redirect:/system/menu/list";
    }

}
